﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;

namespace Ex2.StudentsInfo
{

    public class StudentsInfo
    {
        public static string FILE_NAME = "..\\..\\studentsInfo.bin";

        public static void AddStudent(Student newStudent, string fileName)
        {
            using (BinaryWriter writer = new BinaryWriter(new FileStream(fileName, FileMode.Append)))
            {
                writer.Write(newStudent.FirstName);
                writer.Write(newStudent.LastName);
                writer.Write(newStudent.FacultyNumber);
            }
        }

        public static void PrintAllStudents(string fileName)
        {
            using (BinaryReader reader = new BinaryReader(new FileStream(fileName, FileMode.Open, FileAccess.Read)))
            {
                string firstName;
                string lastName;
                int facultyNumber;
                while (reader.PeekChar() != -1)
                {
                    firstName = reader.ReadString();
                    lastName = reader.ReadString();
                    facultyNumber = reader.ReadInt32();
                    Console.WriteLine("{0} {1}, FN: {2}", firstName, lastName, facultyNumber);
                }
            }
        }

        public static void DeleteStudentByFN(int fn, string fileName)
        {
            string tempFileName = "temp";
            File.Create(tempFileName).Dispose();
            BinaryReader reader = new BinaryReader(new FileStream(fileName, FileMode.Open, FileAccess.Read));
            BinaryWriter writer = new BinaryWriter(new FileStream(tempFileName, FileMode.Open, FileAccess.Write));
            using (reader)
            {
                using (writer)
                {
                    string firstName;
                    string lastName;
                    int facultyNumber;
                    while (reader.PeekChar() != -1)
                    {
                        firstName = reader.ReadString();
                        lastName = reader.ReadString();
                        facultyNumber = reader.ReadInt32();
                        if (facultyNumber != fn)
                        {
                            writer.Write(firstName);
                            writer.Write(lastName);
                            writer.Write(facultyNumber);
                        }
                    }
                }
            }
            File.Replace(tempFileName, fileName, "backUp.bak");
        }

        public static void CreateFile(string fileName)
        {
            File.Create(fileName).Dispose();
        }

        public static void Main()
        {
            CreateFile(FILE_NAME);
            Student gosho = new Student("Gosho", "Petrov", 61250);
            Student pesho = new Student("Pesho", "Obreikov", 61325);
            AddStudent(gosho, FILE_NAME);
            AddStudent(pesho, FILE_NAME);
            PrintAllStudents(FILE_NAME);
            Console.WriteLine();
            DeleteStudentByFN(61325, FILE_NAME);
            PrintAllStudents(FILE_NAME);
        }
    }
}
